package com.zhuannimei.utility;

import org.json.JSONException;

import android.content.Context;

import com.zhuannimei.GlobalApplication;
import com.zhuannimei.domain.ZnmMessage;
import com.zhuannimei.parser.MessageParser;
import com.zhuannimei.ui.MessageActivity;
import com.zhuannimei.ui.TabFourActivity;
import com.zhuannimei.ui.TabOneActivity;

/**
 * 处理消息的工具类
 * 
 * @author Jack 2013-12-27
 * 
 * 
 */
public class MessageUtil {

	public static final String MESSAGE_UNREADNUM = "MESSAGE_UNREADNUM";

	private static int getUnreadNum() {
		return (PersistTool.getInt(MESSAGE_UNREADNUM) + 1);
	}

	public static void doMessage(String msg, Context context)
			throws JSONException {
		ZnmMessage off = new MessageParser().parseJSON(msg);
		switch (getMsgType(off)) {
		case OFFICAL_N:
			// 官方通知
			break;
		case DETAIL_N:
			// 订单审核通过通知
			MessageHandlerList.sendMessage(MessageActivity.class,
					MessageTypes.UPDATE_MESSAGE_LIST, off);// 更新界面
			// 存储数据库
			MessageHandlerList.sendMessage(TabOneActivity.class,
					MessageTypes.UPDATE_MESSAGE_NOTICE);
			MessageHandlerList.sendMessage(TabFourActivity.class,
					MessageTypes.UPDATE_MESSAGE_NOTICE);
			PersistTool.saveInt(MESSAGE_UNREADNUM, getUnreadNum());
			try {
				GlobalApplication.appDB.save(off);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case POINT_N:
			// 每日收益通知

			break;
		case LUCKY_N:
			// 抽奖结果通知
			MessageHandlerList.sendMessage(MessageActivity.class,
					MessageTypes.UPDATE_MESSAGE_LIST, off);// 更新界面
			MessageHandlerList.sendMessage(TabOneActivity.class,
					MessageTypes.UPDATE_MESSAGE_NOTICE);
			MessageHandlerList.sendMessage(TabFourActivity.class,
					MessageTypes.UPDATE_MESSAGE_NOTICE);
			PersistTool.saveInt(MESSAGE_UNREADNUM, getUnreadNum());
			// 存储数据库
			try {
				GlobalApplication.appDB.save(off);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case RANKING_N:
			// 积分榜通知
			// 抽奖结果通知
			MessageHandlerList.sendMessage(MessageActivity.class,
					MessageTypes.UPDATE_MESSAGE_LIST, off);// 更新界面
			MessageHandlerList.sendMessage(TabOneActivity.class,
					MessageTypes.UPDATE_MESSAGE_NOTICE);
			MessageHandlerList.sendMessage(TabFourActivity.class,
					MessageTypes.UPDATE_MESSAGE_NOTICE);
			PersistTool.saveInt(MESSAGE_UNREADNUM, getUnreadNum());
			// 存储数据库
			try {
				GlobalApplication.appDB.save(off);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case PROMOTE_N:
			// 推广通知
			break;

		default:
			break;
		}

	}

	public enum PUSHMESSAGE_TYPE {
		// 官方通知，订单审核通知，今日获取的统计积分，抽奖结果通知，积分榜通知，推广通知
		OFFICAL_N, DETAIL_N, POINT_N, LUCKY_N, RANKING_N, PROMOTE_N
	}

	public static PUSHMESSAGE_TYPE getMsgType(ZnmMessage off) {
		// 官方通知
		if ("0".equals(off.getType())) {
			return PUSHMESSAGE_TYPE.OFFICAL_N;
		}
		// 订单审核通知
		if ("1".equals(off.getType())) {
			return PUSHMESSAGE_TYPE.DETAIL_N;
		}

		// 抽奖结果通知
		if ("2".equals(off.getType())) {
			return PUSHMESSAGE_TYPE.LUCKY_N;
		}
		// 用户每日获取的积分通知
		if ("4".equals(off.getType())) {
			return PUSHMESSAGE_TYPE.POINT_N;
		}
		// 积分榜通知
		if ("3".equals(off.getType())) {
			return PUSHMESSAGE_TYPE.RANKING_N;
		}
		// 推广通知
		if ("5".equals(off.getType())) {
			return PUSHMESSAGE_TYPE.PROMOTE_N;
		}

		return PUSHMESSAGE_TYPE.OFFICAL_N;

	}

}
